﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Nizm0.QOT.Library.Generators
{
    class Pesel
    {
        private int[] PeselArr = new int[11];

        public void GetCheckSum()
        {
            int sum = PeselArr[0] * 1 + PeselArr[1] * 3 + PeselArr[2] * 7 + PeselArr[3] * 9 + PeselArr[4] * 1 + PeselArr[5] * 3 + PeselArr[6] * 7 + PeselArr[7] * 9 + PeselArr[8] * 1 + PeselArr[9] * 3;
            int mod10 = sum % 10;
            sum = 10 - mod10;
            mod10 = sum % 10;
            PeselArr[10] = mod10;
        }

        public string GenPesel(int sex)
        {
            Random rand = new Random(DateTime.Now.GetHashCode());
            int year = rand.Next(Convert.ToInt32(DateTime.Now.AddYears(-60).ToString("yyyy")), Convert.ToInt32(DateTime.Now.AddYears(-18).ToString("yyyy")));
            int month = rand.Next(1, 12);
            int day = rand.Next(1, 28);

            if (year >= 2000)
            {
                year = 2002;
                month = month + 20;
            }

            string smonth = month.ToString();
            string sday = day.ToString();

            if (smonth.Length < 2)
            {
                smonth = "0" + smonth;
            }

            if (sday.Length < 2)
            {
                sday = "0" + sday;
            }

            string number = year.ToString().Substring(2, 2) + smonth + sday + GenSex(sex);

            char[] numb = number.ToCharArray();
            int index = 0;
            foreach (char x in numb)
            {
                PeselArr[index] = Convert.ToInt32(x.ToString());
                index++;
            }
            GetCheckSum();

            string ret = "";

            foreach (int i in PeselArr)
            {
                ret = ret + i.ToString();
            }

            return ret;
        }

        public string GenPesel(int sex, DateTime date)
        {
            int year = date.Year;//Convert.ToInt32(date.ToString("yyyy"));
            int month = date.Month;//Convert.ToInt32(date.ToString("MM"));
            if (year >= 2000)
            {
                //year = 2002;
                month = month + 20;
            }

            string smonth = month.ToString();
            string sday = date.Day.ToString();//.ToString("dd");

            if (smonth.Length < 2)
            {
                smonth = "0" + smonth;
            }

            if (sday.Length < 2)
            {
                sday = "0" + sday;
            }

            string number = year.ToString().Substring(2, 2) + smonth + sday + GenSex(sex);

            char[] numb = number.ToCharArray();
            int index = 0;
            foreach (char x in numb)
            {
                PeselArr[index] = Convert.ToInt32(x.ToString());
                index++;
            }
            GetCheckSum();

            string ret = "";

            foreach (int i in PeselArr)
            {
                ret = ret + i.ToString();
            }

            return ret;
        }

        public string GenPesel(int sex, string start, string stop)
        {
            string ret = "";
            if (start != "" && stop != "")
            {
                Random rand = new Random(DateTime.Now.GetHashCode());
                int year = rand.Next(Convert.ToInt32(start), Convert.ToInt32(stop));
                int month = rand.Next(1, 12);
                int day = rand.Next(1, 28);

                if (year >= 2000)
                {
                    year = 2002;
                    month = month + 20;
                }

                string smonth = month.ToString();
                string sday = day.ToString();

                if (smonth.Length < 2)
                {
                    smonth = "0" + smonth;
                }

                if (sday.Length < 2)
                {
                    sday = "0" + sday;
                }

                string number = year.ToString().Substring(2, 2) + smonth + sday + GenSex(sex);

                char[] numb = number.ToCharArray();
                int index = 0;
                foreach (char x in numb)
                {
                    PeselArr[index] = Convert.ToInt32(x.ToString());
                    index++;
                }
                GetCheckSum();



                foreach (int i in PeselArr)
                {
                    ret = ret + i.ToString();
                }
            }

            return ret;
        }

        private int GenSex(int sex)
        {
            Random rand = new Random(DateTime.Now.GetHashCode());

            int ret;
            do
            {
                ret = rand.Next(1111, 9999);
            }
            while (!((ret % 2) == sex));

            return ret;
        }

    }
}
